package net.htjs.juc.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 任务串联，链式组合
 */
@Slf4j
public class Demo2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 有返回值的任务
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            log.info("异步任务执行");
            return "hello";
        });
        log.info("结果是:{}", future2.get());
        log.info("结果是:{}", future2.getNow("默认值"));
        CompletableFuture<Void> processed = future2
                // 转换结果
                .thenApply(s -> s + "处理后")
                // 消费结果
                .thenAccept(System.out::println)
                .thenRun(() -> log.info("任务结束"));
        processed.join();
    }

}
